import { AnyAction, Reducer } from 'redux';
import { message } from 'antd';
import { EffectsCommandMap } from 'dva';
import { fetchValidation } from '@/utils';
import { querySearchList, querySaveList } from './service';

export type Effect = (
  action: AnyAction,
  effects: EffectsCommandMap & { select: <T>(func: (state: {}) => T) => T },
) => void;

export interface ModelType {
  namespace: string;
  state: {};
  effects: {
    fetch: Effect;
    saveSorting: Effect;
  };
  reducers: {
    save: Reducer
  }
}


const Model: ModelType = {
  namespace: 'tookManagementSetting',

  state: {
    sortingExpress: {},
  },

  effects: {
    * fetch({ payload }, { call, put }) {
      const response = yield call(querySearchList, payload);
      const data = fetchValidation(response);
      yield data && put({
        type: 'save',
        payload: { sortingExpress: data.body },
      });
    },

    * saveSorting({ payload }, { call }) {
      const response = yield call(querySaveList, payload);
      const data = fetchValidation(response);
      if (data) {
        message.success('提交成功');
      }
    },
  },


  reducers: {
    save(state, { payload }) {
      return {
        ...state,
        ...payload,
      };
    },
  },
};

export default Model;
